package nige.d3.test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JFrame;

import nige.tools.Time;

public class Experience extends JFrame implements KeyListener {
	RenderingThread renderingThread = new RenderingThread();
	Graphics buffer;
	Image image; 
	int x = 0;
	int polyTotal = 0;
	
	public Experience(){
		setSize( 400, 400 );
		setVisible(true);
		addKeyListener(this);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		renderingThread.start();
	}
	
	public void update(Graphics g){
		paint(g);
	}
	
	public void paint(Graphics g){
		if(buffer==null){
			image = createImage(400,400);
			buffer = image.getGraphics();
		}
		buffer.setColor( Color.white );
		buffer.fillRect( 0, 0, 400, 400 );
		int dep = Time.getMs();
		Random ran = new Random();
		Image img = Toolkit.getDefaultToolkit().getImage(
				"C:/Documents and Settings/ngenevaux/Mes documents/Mes images/caravane.jpeg"
				);
		img = Toolkit.getDefaultToolkit().getImage("C:/tmp/la-mort.gif");
		//buffer.drawImage(img,50,50,null);
		//buffer.drawImage(img,150,200,300,300,0,0,100,100,null);
		for(int i=0;i<polyTotal;i++) {
			buffer.setColor( new Color(ran.nextInt(200),ran.nextInt(200),
					ran.nextInt(200)) );
			buffer.drawRect(ran.nextInt(400),ran.nextInt(400),
					ran.nextInt(100),ran.nextInt(100));
		}
		buffer.setColor( new Color(0,0,0) );
		buffer.drawString("Temps d'affichage : "+(Time.getMs()-dep),10,50);
		buffer.drawString("Poly total : "+polyTotal,10,80);
		int[] xPoints = new int[4];
		int[] yPoints = new int[4];
		
		xPoints[0] = 130;
		yPoints[0] = 230;
		
		xPoints[1] = 200;
		yPoints[1] = 230;
		
		xPoints[2] = 230;
		yPoints[2] = 250;
		
		xPoints[3] = 100;
		yPoints[3] = 250;
		
		buffer.drawPolygon(xPoints,yPoints,4);
		
		x++;
		if(x>400) x = 0;
		g.drawImage(image, 0, 0, this);
	}
	
	class RenderingThread extends Thread {
		public void run(){
			while(true){
				try {
					repaint(); 
					sleep(20);
				}
				catch (Exception e) {
				} 
			}
		}
	}
	
	public static void main(String[] args){
		new Experience();
	}

	public void keyPressed(KeyEvent arg0) {
		System.out.println(arg0);
		if(arg0.getKeyCode()==111) {
			polyTotal /= 10;
		}
		if(arg0.getKeyCode()==106) {
			polyTotal *= 10;
		}
		if(arg0.getKeyCode()==107) {
			polyTotal += 10;
		}
		if(arg0.getKeyCode()==109) {
			polyTotal = (polyTotal<10)? 0 : polyTotal-10;
		}
	}

	public void keyReleased(KeyEvent arg0) {
	}

	public void keyTyped(KeyEvent arg0) {
	}
}
